package com.yao.grpcdemo.test;

import com.yao.grpcdemo.proto.GreeterGrpc;
import com.yao.grpcdemo.proto.GreeterService;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

public class GRpcClient {
    private GreeterGrpc.GreeterStub greeterStub;
    private ManagedChannel managedChannel;
    private GreeterGrpc.GreeterBlockingStub greeterBlockingStub;

    public GRpcClient() {
        this.managedChannel = ManagedChannelBuilder.forAddress("localhost", 10086)
                .usePlaintext().build();
        this.greeterStub = GreeterGrpc.newStub(managedChannel);
        this.greeterBlockingStub = GreeterGrpc.newBlockingStub(managedChannel);
    }

    public void sayHello() {
        greeterStub.sayHello(GreeterService.HelloRequest.newBuilder().setName("yao").build(), new StreamObserver<GreeterService.HelloReply>() {
            @Override
            public void onNext(GreeterService.HelloReply helloReply) {
                System.out.println(helloReply.getMessage());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {

            }
        });
    }

    public void hello() {
        GreeterService.HelloReply reply = greeterBlockingStub.sayHello(GreeterService.HelloRequest.newBuilder().setName("yao").build());
        System.out.println(reply.getMessage());

    }
}
